package cn.iocoder.yudao.module.cmt.service.activity;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.module.cmt.controller.admin.activity.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.activity.ActivityInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cmt.dal.mysql.activity.ActivityInfoMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cmt.enums.ErrorCodeConstants.*;

@Service
@Validated
public class ActivityInfoServiceImpl implements ActivityInfoService {

    @Resource
    private ActivityInfoMapper activityInfoMapper;

    @Override
    public Long createActivityInfo(ActivityInfoSaveReqVO createReqVO) {
        ActivityInfoDO activityInfo = BeanUtils.toBean(createReqVO, ActivityInfoDO.class);
        activityInfoMapper.insert(activityInfo);
        return activityInfo.getId();
    }

    @Override
    public void updateActivityInfo(ActivityInfoSaveReqVO updateReqVO) {
        validateActivityInfoExists(updateReqVO.getId());
        ActivityInfoDO updateObj = BeanUtils.toBean(updateReqVO, ActivityInfoDO.class);
        activityInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityInfo(Long id) {
        validateActivityInfoExists(id);
        activityInfoMapper.deleteById(id);
    }

    private void validateActivityInfoExists(Long id) {
        if (activityInfoMapper.selectById(id) == null) {
            throw exception(ACTIVITY_INFO_NOT_EXISTS);
        }
    }

    @Override
    public ActivityInfoDO getActivityInfo(Long id) {
        return activityInfoMapper.selectById(id);
    }

    @Override
    public PageResult<ActivityInfoDO> getActivityInfoPage(ActivityInfoPageReqVO pageReqVO) {
        return activityInfoMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteActivityInfoListByIds(List<Long> ids) {
        ids.forEach(this::validateActivityInfoExists);
        activityInfoMapper.deleteBatchIds(ids);
    }

}
